﻿using System;
using System.Collections.Generic;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TracNghiemV1.Database;

namespace TracNghiemV1.DAO
{
    class TopicDao
    {
        private static TestDataDataContext m_dataContext = null;

        public static IList<TOPIC> GetTopicList()
        {
            return DataContextDao.GetInstance().TOPICs.ToList();
        }

        public static void AddTopic(TOPIC newTopic)
        {

            m_dataContext = DataContextDao.GetInstance();

            m_dataContext.TOPICs.InsertOnSubmit(newTopic);
            m_dataContext.SubmitChanges();
        }

        public static void DeleteTopic(string topicId)
        {
            m_dataContext = DataContextDao.GetInstance();

            var topic = m_dataContext.TOPICs.Single(u => u.TOPIC_ID == topicId);
            m_dataContext.TOPICs.DeleteOnSubmit(topic);
            m_dataContext.SubmitChanges();
        }

        public static void ModifyTopic(TOPIC modifiedTopic)
        {
            m_dataContext = DataContextDao.GetInstance();

            var topic = m_dataContext.TOPICs.Single(u => u.TOPIC_ID == modifiedTopic.TOPIC_ID);

            topic.TOPIC_NAME = modifiedTopic.TOPIC_NAME;
            topic.TOPIC_DESC = modifiedTopic.TOPIC_DESC;

            m_dataContext.SubmitChanges();
        }

        public static TOPIC FindTopicWithTopicId(string topicId)
        {
            m_dataContext = DataContextDao.GetInstance();
            return m_dataContext.TOPICs.Single(u => u.TOPIC_ID == topicId);
        }

        public static IList<TOPIC> FindTopicListWithName(string name)
        {
            m_dataContext = DataContextDao.GetInstance();

            var topics = from p in m_dataContext.TOPICs
                        where SqlMethods.Like(p.TOPIC_NAME, "%" + name + "%")
                        select p;
            return topics.ToList();
        }

        public static TOPIC GetLatestTopic()
        {
            m_dataContext = DataContextDao.GetInstance();
            return m_dataContext.TOPICs.ToList().Last();
        }
    }
}
